{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0.97923874  0.98291324  0.58754519  0.15573866  0.0943834   0.49569611\n",
      "  0.02680417  0.42657534  0.219299    0.28602307  0.13371154  0.96354797\n",
      "  0.06824282  0.93176477  0.4732012   0.29696019  0.58412791  0.42032096\n",
      "  0.44569646  0.43460511  0.07135405  0.02014773  0.67988333  0.32726885\n",
      "  0.13887713  0.89226162  0.41667841  0.38011182  0.01181817  0.36695354\n",
      "  0.7321678   0.21846748  0.20427656  0.1838937   0.22158314  0.06066658\n",
      "  0.90578263  0.42709457  0.85636651  0.9889025   0.93674254  0.89581439\n",
      "  0.14879678  0.16849397  0.80672073  0.46879498  0.8017066   0.68338707\n",
      "  0.51719765  0.08950576  0.63859256  0.15475313  0.65321094  0.4729362\n",
      "  0.39959724  0.11666489  0.63063491  0.53781309  0.14437659  0.56948525\n",
      "  0.39340454  0.55896918  0.71548919  0.13508621  0.68893505  0.90876232\n",
      "  0.40707155  0.37828051  0.28519617  0.11896658  0.85251718  0.04877315\n",
      "  0.21665071  0.29126592  0.91776375  0.54111161  0.6978458   0.56628118\n",
      "  0.86145398  0.20517336  0.85875283  0.25035213  0.72023376  0.28515087\n",
      "  0.2140825   0.56287222  0.01259732  0.23254208  0.95809864  0.05170935\n",
      "  0.63649926  0.65920319  0.5659788   0.12024349  0.62695416  0.15412372\n",
      "  0.15700501  0.23263417  0.42898621  0.67850896  0.18191683  0.19263446\n",
      "  0.62956299  0.30025348  0.1751894   0.10719835  0.89202196  0.6079847\n",
      "  0.12063848  0.54513758  0.26783858  0.98794774  0.36585955  0.78638496\n",
      "  0.39732268  0.61879635  0.83065206  0.63816701  0.57191898  0.608993\n",
      "  0.81027682  0.54219566  0.07012528  0.25876234  0.64559136  0.82578053\n",
      "  0.7697945   0.96972978  0.41562797  0.22032481  0.676813    0.01723156\n",
      "  0.06166241  0.5615573   0.51266052  0.16418685  0.8509699   0.01866088\n",
      "  0.61459646  0.78197669  0.883138    0.82010935  0.93317697  0.70702567\n",
      "  0.67697633  0.75631462  0.12043837  0.21021067  0.24682098  0.79406994\n",
      "  0.04939074  0.39986323  0.7056639   0.04394648  0.88013992  0.05475362\n",
      "  0.05459558  0.73141152  0.52288871  0.45605925  0.65320496  0.01220833\n",
      "  0.49005698  0.37580294  0.73559397  0.84595575  0.7274806   0.16064655\n",
      "  0.026445    0.36632239  0.73430698  0.4302263   0.8676449   0.42654983\n",
      "  0.53144499  0.52348029  0.53241803  0.55689536  0.74884445  0.83594333\n",
      "  0.59371709  0.75465743  0.85723495  0.47933865  0.68428913  0.83008101\n",
      "  0.14859455  0.46613075  0.17395508  0.67754326  0.56841379  0.39029115\n",
      "  0.19425479  0.32500024  0.6746342   0.16024989  0.6485772   0.32787829\n",
      "  0.21355476  0.71961286]\n",
      "0 [0.2914916, 0.16786821]\n",
      "1 [0.4343999, 0.25954521]\n",
      "2 [0.50267619, 0.31271484]\n",
      "3 [0.53351068, 0.34633458]\n",
      "4 [0.5456053, 0.3699435]\n",
      "5 [0.54836345, 0.38834384]\n",
      "6 [0.54651225, 0.40395531]\n",
      "7 [0.54242682, 0.41799921]\n",
      "8 [0.53729951, 0.43109411]\n",
      "9 [0.53172797, 0.44355527]\n",
      "10 [0.52601063, 0.45554528]\n",
      "11 [0.52029556, 0.46714988]\n",
      "12 [0.51465529, 0.47841606]\n",
      "13 [0.5091244, 0.48937115]\n",
      "14 [0.50371844, 0.5000326]\n",
      "15 [0.49844334, 0.51041275]\n",
      "16 [0.49330038, 0.52052122]\n",
      "17 [0.48828849, 0.5303663]\n",
      "18 [0.48340541, 0.53995538]\n",
      "19 [0.47864839, 0.54929537]\n",
      "20 [0.47401449, 0.55839294]\n",
      "21 [0.46950063, 0.56725442]\n",
      "22 [0.46510381, 0.57588601]\n",
      "23 [0.46082097, 0.58429366]\n",
      "24 [0.45664924, 0.59248322]\n",
      "25 [0.45258573, 0.60046035]\n",
      "26 [0.44862759, 0.60823053]\n",
      "27 [0.44477212, 0.61579913]\n",
      "28 [0.44101667, 0.62317145]\n",
      "29 [0.43735865, 0.6303525]\n",
      "30 [0.43379551, 0.63734728]\n",
      "31 [0.43032479, 0.64416063]\n",
      "32 [0.42694411, 0.65079719]\n",
      "33 [0.42365113, 0.65726161]\n",
      "34 [0.42044356, 0.66355836]\n",
      "35 [0.41731921, 0.66969174]\n",
      "36 [0.41427591, 0.67566603]\n",
      "37 [0.41131154, 0.68148535]\n",
      "38 [0.40842408, 0.6871537]\n",
      "39 [0.40561152, 0.69267499]\n",
      "40 [0.40287191, 0.69805312]\n",
      "41 [0.40020338, 0.70329171]\n",
      "42 [0.39760405, 0.70839441]\n",
      "43 [0.39507216, 0.71336472]\n",
      "44 [0.39260596, 0.71820611]\n",
      "45 [0.39020371, 0.72292191]\n",
      "46 [0.38786379, 0.7275154]\n",
      "47 [0.38558456, 0.73198974]\n",
      "48 [0.38336447, 0.73634797]\n",
      "49 [0.38120195, 0.74059319]\n",
      "50 [0.37909555, 0.74472827]\n",
      "51 [0.37704378, 0.74875605]\n",
      "52 [0.37504524, 0.75267941]\n",
      "53 [0.37309852, 0.75650096]\n",
      "54 [0.37120232, 0.76022339]\n",
      "55 [0.36935529, 0.76384926]\n",
      "56 [0.36755618, 0.76738107]\n",
      "57 [0.36580375, 0.77082127]\n",
      "58 [0.36409676, 0.77417225]\n",
      "59 [0.36243406, 0.77743626]\n",
      "60 [0.36081448, 0.78061563]\n",
      "61 [0.35923693, 0.78371251]\n",
      "62 [0.35770029, 0.78672904]\n",
      "63 [0.35620353, 0.78966737]\n",
      "64 [0.3547456, 0.79252946]\n",
      "65 [0.35332546, 0.79531729]\n",
      "66 [0.35194218, 0.79803282]\n",
      "67 [0.35059476, 0.8006779]\n",
      "68 [0.34928229, 0.80325437]\n",
      "69 [0.34800389, 0.80576396]\n",
      "70 [0.34675866, 0.80820847]\n",
      "71 [0.34554574, 0.81058955]\n",
      "72 [0.34436426, 0.81290889]\n",
      "73 [0.34321344, 0.81516808]\n",
      "74 [0.34209245, 0.81736869]\n",
      "75 [0.34100056, 0.81951219]\n",
      "76 [0.33993697, 0.82160008]\n",
      "77 [0.33890098, 0.82363379]\n",
      "78 [0.33789188, 0.82561475]\n",
      "79 [0.33690897, 0.82754433]\n",
      "80 [0.33595154, 0.82942384]\n",
      "81 [0.33501893, 0.8312546]\n",
      "82 [0.33411053, 0.83303791]\n",
      "83 [0.3332257, 0.83477491]\n",
      "84 [0.33236381, 0.83646691]\n",
      "85 [0.33152428, 0.83811498]\n",
      "86 [0.33070654, 0.83972031]\n",
      "87 [0.32990998, 0.84128398]\n",
      "88 [0.32913411, 0.84280711]\n",
      "89 [0.32837835, 0.84429073]\n",
      "90 [0.3276422, 0.84573585]\n",
      "91 [0.32692516, 0.84714347]\n",
      "92 [0.32622671, 0.84851462]\n",
      "93 [0.32554638, 0.84985018]\n",
      "94 [0.3248837, 0.85115111]\n",
      "95 [0.32423818, 0.85241824]\n",
      "96 [0.32360944, 0.85365254]\n",
      "97 [0.322997, 0.85485482]\n",
      "98 [0.32240045, 0.85602587]\n",
      "99 [0.32181937, 0.85716659]\n",
      "100 [0.32125336, 0.85827768]\n",
      "101 [0.32070205, 0.85935998]\n",
      "102 [0.32016504, 0.86041421]\n",
      "103 [0.31964195, 0.86144108]\n",
      "104 [0.31913242, 0.8624413]\n",
      "105 [0.31863612, 0.8634156]\n",
      "106 [0.3181527, 0.86436462]\n",
      "107 [0.31768182, 0.86528903]\n",
      "108 [0.31722313, 0.86618942]\n",
      "109 [0.31677637, 0.8670665]\n",
      "110 [0.31634116, 0.86792082]\n",
      "111 [0.31591725, 0.86875296]\n",
      "112 [0.31550437, 0.86956352]\n",
      "113 [0.31510219, 0.87035304]\n",
      "114 [0.31471044, 0.87112212]\n",
      "115 [0.31432882, 0.87187123]\n",
      "116 [0.31395712, 0.87260091]\n",
      "117 [0.31359506, 0.87331164]\n",
      "118 [0.31324241, 0.87400395]\n",
      "119 [0.3128989, 0.87467831]\n",
      "120 [0.31256428, 0.87533516]\n",
      "121 [0.31223837, 0.87597495]\n",
      "122 [0.31192091, 0.87659818]\n",
      "123 [0.31161168, 0.87720525]\n",
      "124 [0.31131047, 0.87779653]\n",
      "125 [0.31101707, 0.87837249]\n",
      "126 [0.31073129, 0.87893349]\n",
      "127 [0.31045291, 0.87947994]\n",
      "128 [0.31018177, 0.88001227]\n",
      "129 [0.30991766, 0.88053077]\n",
      "130 [0.30966038, 0.8810358]\n",
      "131 [0.30940977, 0.88152772]\n",
      "132 [0.30916569, 0.88200688]\n",
      "133 [0.30892792, 0.88247365]\n",
      "134 [0.30869633, 0.88292831]\n",
      "135 [0.30847076, 0.88337117]\n",
      "136 [0.30825102, 0.88380253]\n",
      "137 [0.30803698, 0.88422269]\n",
      "138 [0.30782849, 0.88463193]\n",
      "139 [0.30762541, 0.88503057]\n",
      "140 [0.30742761, 0.88541889]\n",
      "141 [0.30723494, 0.88579714]\n",
      "142 [0.30704728, 0.88616556]\n",
      "143 [0.30686447, 0.88652444]\n",
      "144 [0.3066864, 0.88687402]\n",
      "145 [0.30651295, 0.88721448]\n",
      "146 [0.306344, 0.88754612]\n",
      "147 [0.30617943, 0.88786918]\n",
      "148 [0.30601916, 0.88818383]\n",
      "149 [0.30586302, 0.88849038]\n",
      "150 [0.30571094, 0.88878894]\n",
      "151 [0.30556279, 0.88907975]\n",
      "152 [0.30541849, 0.88936305]\n",
      "153 [0.30527791, 0.88963896]\n",
      "154 [0.305141, 0.88990772]\n",
      "155 [0.30500764, 0.8901695]\n",
      "156 [0.30487776, 0.89042449]\n",
      "157 [0.30475122, 0.89067286]\n",
      "158 [0.30462798, 0.8909148]\n",
      "159 [0.30450794, 0.89115047]\n",
      "160 [0.304391, 0.89138001]\n",
      "161 [0.30427709, 0.89160365]\n",
      "162 [0.30416614, 0.89182144]\n",
      "163 [0.30405807, 0.89203358]\n",
      "164 [0.30395281, 0.89224023]\n",
      "165 [0.30385029, 0.89244151]\n",
      "166 [0.30375043, 0.89263761]\n",
      "167 [0.30365312, 0.89282858]\n",
      "168 [0.30355835, 0.89301461]\n",
      "169 [0.30346605, 0.89319581]\n",
      "170 [0.30337614, 0.8933723]\n",
      "171 [0.30328858, 0.89354426]\n",
      "172 [0.30320325, 0.89371175]\n",
      "173 [0.30312017, 0.89387488]\n",
      "174 [0.30303922, 0.89403379]\n",
      "175 [0.30296037, 0.89418852]\n",
      "176 [0.30288357, 0.89433926]\n",
      "177 [0.30280876, 0.89448613]\n",
      "178 [0.30273589, 0.89462918]\n",
      "179 [0.30266494, 0.89476848]\n",
      "180 [0.30259579, 0.8949042]\n",
      "181 [0.30252847, 0.8950364]\n",
      "182 [0.30246288, 0.89516515]\n",
      "183 [0.30239898, 0.89529055]\n",
      "184 [0.30233675, 0.89541274]\n",
      "185 [0.30227613, 0.89553171]\n",
      "186 [0.3022171, 0.89564764]\n",
      "187 [0.30215958, 0.89576054]\n",
      "188 [0.30210355, 0.89587051]\n",
      "189 [0.30204898, 0.89597762]\n",
      "190 [0.30199584, 0.89608198]\n",
      "191 [0.30194408, 0.89618361]\n",
      "192 [0.30189365, 0.89628261]\n",
      "193 [0.30184454, 0.89637905]\n",
      "194 [0.30179667, 0.89647299]\n",
      "195 [0.30175006, 0.89656448]\n",
      "196 [0.30170467, 0.89665359]\n",
      "197 [0.30166045, 0.89674038]\n",
      "198 [0.30161738, 0.89682496]\n",
      "199 [0.30157542, 0.89690733]\n"
     ]
    }
   ],
   "source": [
    "#原始数据集\n",
    "x_data = np.random.rand(200)\n",
    "y_data = x_data*0.9+0.3\n",
    "print(x_data)\n",
    "#线性模型\n",
    "b = tf.Variable(0.)\n",
    "k = tf.Variable(0.)\n",
    "y = k*x_data+b\n",
    "\n",
    "#代价函数\n",
    "loss = tf.reduce_mean(tf.square(y-y_data))\n",
    "#优化器\n",
    "optimizer = tf.train.GradientDescentOptimizer(0.2)\n",
    "#最小化代价函数\n",
    "train = optimizer.minimize(loss)\n",
    "\n",
    "init = tf.global_variables_initializer()\n",
    "with tf.Session() as sess:\n",
    "    sess.run(init)\n",
    "    for step in range(200):\n",
    "        sess.run(train)\n",
    "        print(step,sess.run([b,k]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
